var BeforeSell = function(){
	
	var userSelector = null;
	var selectUserId = [];
	var userSelector2 = null;
	var selectUserId2 = [];
	var project_id ='-1';	
	var ps = new ProjectSourceDemo();	
	
	// 查询条件---begin
	var searchAFcustomerID = new Ext.form.TextField({
		fieldLabel : '客户编号',
		width : 150
	});
	var searchAFcustomerName = new Ext.form.TextField({
		fieldLabel : '客户名称',
		width : 150
	});
	
	var searchAfterServiceID = new Ext.form.TextField( {
		fieldLabel : '回访单编号',
		width : 150
	});
	
	var searchOrderByTime = new Ext.form.DateField( {
		fieldLabel : '配发日期',
		format : 'Y-m-d',
		width : 150
	});
	
	var btn_search = new Ext.Button({
		text : '查询',
		iconCls: 'icon-search',
		handler: function (){
			searchOrderByPerson.load();	
			}
	});
	// 查询条件---end
	
	// 跟踪记录信息---begin

	var personAfterServiceID = new Ext.form.TextField({
		fieldLabel : '回访单编号',
		width : 150,
		disabled:true
	});
	
	var personAfterServiceCustomer = new Ext.form.TextField({
		fieldLabel : '客户名称',
		width : 300,
		allowBlank : false,
		disabled:true
	});
	
	var personAfterServiceCustomerArea = new Ext.form.ComboBox( {
		fieldLabel : '所属区域',
		store: OMS.AREA,
		mode : 'local',
		displayField : 'name',
		triggerAction : 'all',
		editable : false,
		valueField : 'id',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	
	
	
	var projectBus = new Ext.form.TextArea({
		fieldLabel : '联系主题',
		width : 800
	});
	var projectCreateDate = new Ext.form.DateField( {
		fieldLabel : '维修日期',
		format : 'Y-m-d',
		width : 150
	});
	
	var projectComplateDate = new Ext.form.DateField( {
		fieldLabel : '计划拜访日期',
		format : 'Y-m-d',
		width : 150
	});
	
	var planUper = new Ext.form.TextField({
		fieldLabel : '提交人',
		width : 150
	});
	

	
	var projectHeader = new Ext.form.TextField({
		fieldLabel : '参与者',
		width : 150
	});
	
	var projectAssigner = new Ext.form.TextField({
		fieldLabel : '维修责任人',
		width : 150,
		allowBlank : false,
		listeners: {
			   'render': function(cmp) {
					var self = cmp.ownerCt.ownerCt;
					cmp.getEl().on('click', function(){showUser2();}, self); 
				}
		}
	});
	

	
	var projectNewlast = new Ext.form.TextArea({
		fieldLabel : '最新进展',
		width : 800
	});
	
	var projectDangerous = new Ext.form.TextArea({
		fieldLabel : '困难及支撑',
		width : 800
	});
	
	var projectNextPlan = new Ext.form.TextArea({
		fieldLabel : '部门经理意见',
		width : 800
	});
	var bossAdvice = new Ext.form.TextArea({
		fieldLabel : '总经理意见',
		width : 800
	});

	
	var fileUpload = new Ext.form.FileUploadField({
		fieldLabel : '上传附件',
		emptyText: '请选择要上传的文件',
		width : 520,
		name : 'fileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});
	var strackStore = new Ext.data.JsonStore(
			{
				url : '../project/searchProjectTrack.form?timesnap=' + new Date().getTime(),
				baseParams: {projectId: '-1'},
				root : 'result',
				fields : [ 'PROJECT_ID', 'USER_ID', 'PROJECT_GROUP','LATEST_MESSAGE', 'DIFFICULT', 'NEXT_PLAN','CREATE_DT', 'FILE_PATH','USER_NAME'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = { projectId : project_id };
					}
				}
			});
	var followGrid = new Ext.grid.GridPanel({
		width : 900,
		height : 160,
		store : strackStore,
		columns : [{header:'责任人',width: 150,dataIndex:'USER_NAME'},
				   {header:'最新进展',width: 150,dataIndex:'USER_NAME'},
				   {header:'困难及难点',width: 150,dataIndex:'CREATE_DT'},
		           {header:'部门意见',width: 150,dataIndex:'CREATE_DT'},
		           {header:'总经理意见',width: 250,dataIndex:'LATEST_MESSAGE'}
		           ],
		bbar:new Ext.PagingToolbar({
  		   pageSize: 5,
           store: strackStore,
           displayInfo: true,
           displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
           emptyMsg: "没有记录..."
          })
	});
	
	//下载 文件
	var downloadUrl = function(s, m, rec){
		return "<a href='../task/downloadFile.form?fileId="+s+"'>下载</a>";
	};
	//附件列表
	var fileStore = new Ext.data.JsonStore(
			{
				url : '../task/searchTaskUpLoadFile.form?timesnap='+new Date().getTime(),
				baseParams: {exeId: '-1'},
				root : 'result',
				fields: ['ACCESSORY_ID', 'ACCESSORY_NAME', 'UPLOAD_DATE','FILE_SIZE','UPLOAD_NAME','ACCESSORY_DESC','USER_NAME'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = { exeId : project_id };
					}
				}
			});
	var fileGrid = new Ext.grid.GridPanel({
		width : 900,
		height : 160,
		store : fileStore,
		columns : [{header:'附件名称',width: 150,dataIndex:'ACCESSORY_NAME'},
		           {header:'上传日期',width: 150,dataIndex:'UPLOAD_DATE'},
		           {header:'文件大小(K)',width: 250,dataIndex:'FILE_SIZE'},
		           {header:'上传人',width: 250,dataIndex:'USER_NAME'},
		           {header: '操作',width: 50, dataIndex: 'ACCESSORY_ID',renderer:downloadUrl}
		           ],
		bbar:new Ext.PagingToolbar({
			   pageSize: 5,
	         store: fileStore,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
	        })
	});
	
	
	
	
	// 跟踪记录信息---end
	var followRec = new Ext.form.FieldSet({
		title : '跟踪记录',
		collapsible : true,
		autoHeight : true,
		width : 930, 
		items : [followGrid,
		         {layout:'column',defaults:{layout:'form'},
		      		items:[{columnWidth:0.47,items:[personAfterServiceID]},
		      		       {columnWidth:0.47,items:[personAfterServiceCustomer]}]
		         },
		         {layout:'column',defaults:{layout:'form'},
		        	 items:[{columnWidth:0.47,items:[projectCreateDate]}]
		         },projectNewlast,projectDangerous,projectNextPlan,bossAdvice]
	});
	
	var searchOrderByPerson = new Ext.data.JsonStore(
			{
				url : '../afterserivebyperson/searchOrderByPerson.form?status=11&timesnap=' + new Date()
						.getTime(),
				root : 'result',
				fields : [ 'AfterServiceID', 'ServiceDetailId', 'ServicePerson',
						'AFcustomerName', 'AFserviceAddress','AFserviceStartDate'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = {
							searchName : searchAFcustomerID.getValue(),
							searchArea : personAfterServiceCustomerArea.getValue(),
							searchOrderByTime : searchOrderByTime.getValue() ? searchOrderByTime.getValue().format('Y-m-d') : ''
						};
					}
				}
			});
	//取得项目负责人信息路径
	var getProjectAssigner = new Ext.data.Connection({
		url:'../project/getProjectAssigner.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	
	var sellGrid = new Ext.grid.GridPanel({
		width : 930,
		height : 150,
		store : searchOrderByPerson,
		scope:this,
		listeners : {
			scope : this,
			rowmousedown : function(grid,index,e){
				var data = grid.store.getAt(index).data;
				personAfterServiceID.setValue(data.AfterServiceID);
				var servicedetailid=data.ServiceDetailId;
				personAfterServiceCustomer.setValue(data.AFcustomerName);
				}
		},
		columns : [{header:'回访单编号',sortable: true,width: 100,dataIndex:'AfterServiceID'},
		           {header:'客户名称',width: 150, sortable: true,dataIndex:'AFcustomerName'},
		           {header:'维修人员',width: 150, sortable: true,dataIndex:'ServicePerson'},
		           {header:'维修地点',sortable: true,sortable: true,dataIndex:'AFserviceAddress'},
		           {header:'预约时间',sortable: true,sortable: true,dataIndex:'AFserviceStartDate'}
		         ],
       bbar:new Ext.PagingToolbar({
		 pageSize: 5,
         store: searchOrderByPerson,
         displayInfo: true,
         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
         emptyMsg: "没有记录..."
       })
	});

	this.beforeSell = new Ext.form.FormPanel({
		frame : true,
		width : 950,
		fileUpload: true, 
		labelWidth : 85,
		buttons : [
		           {text:'保存',iconCls: 'icon-accept',scope:this,handler:function(){saveTract(this.beforeSell);}},
		           {text:'重置',iconCls: 'icon-return',scope:this,handler:function(){this.beforeSell.getForm().reset();}}
		           ],
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[ {columnWidth:0.3,items:[searchAFcustomerID]},
			      		 {columnWidth:0.3,items:[searchAFcustomerName]},
			             {columnWidth:0.3,items:[personAfterServiceCustomerArea]},
			             {columnWidth:0.3,items:[searchOrderByTime]},
			             {columnWidth:0.1,items:[btn_search]}]
				},sellGrid,followRec],
	   listeners: {
	    	render: {fn:function(){ 
					searchOrderByPerson.load();
			}}
		}
	});
	//为售前控件设置值
	var setCompValueAndDisabledForBeforeSell = function(data){
		projectCode.setValue(data.PROJECT_ID);
		projectName.setValue(data.PROJECT_NAME);
		projectArea.setValue(data.AREA_ID);
		projectType.setValue(data.PROJECT_TYPE);
		projectCreateDate.setValue(data.PROJECT_CREATE_DT);
		projectComplateDate.setValue(data.EST_DT);
		projectHeader.setValue(data.JOIN_LEADER);
		projectBus.setValue(data.OWNER_PROVIDE);
		
		projectCode.disable();
		projectName.disable();
		projectArea.disable();	
		projectType.disable();	
		projectCreateDate.disable();	
		projectComplateDate.disable();	
		projectHeader.disable();
		projectBus.disable();
	};
	//项目负责人控件设置值
	var setProjectAssigner = function(data){
		Utils.request(getProjectAssigner,{
			params:{
				project_id:data.PROJECT_ID
			},
			success:function(dataResult){
				projectAssigner.setValue(dataResult.projectAssigner);
				projectMem.setValue(dataResult.result);
				selectUserId2 = [dataResult.projectAssignerUserIds];
				selectUserId = [dataResult.userNamesUserIds];
			}
		});
	};
	//确定立项信息路径
	var addTractCon = new Ext.data.Connection({
		url:'../afterserivebyperson/updateOrderByPerson.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	
	var addDetail = new Ext.data.Connection({
		url:'../afterserivebyperson/updateOrderByPerson.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var saveTract = function(bs){
		if(servicedetailid == ''){
				alert(servicedetailid);
			Utils.confirm2({
				msg:"是否增加？",
				width:220,
				fn:function()
					{Utils.request(addDetail,{
						params:{
											
							Stage:'1'
							
						},
						success:function(data){
							Utils.info("保存成功","系统提示");	
							}
						})
					}
				})
			}
		}	
			
		
		
		
	//检索项目
	
	var showUser = function(){
		if(userSelector==null){
			userSelector=new Tree.User(selectUserId,function(o){
				setUsers(o);
			},null,'104');
		}
		userSelector.show();
	};
	var setUsers = function(o){
		var names = [];
		selectUserId =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			selectUserId.push(o[i].id);
		}
		projectMem.setValue(names.join(';'));
	};
	
	var showUser2 = function(){
		if(userSelector2==null){
			userSelector2=new Tree.User(selectUserId2,function(o){
				setUsers2(o);
			},null,'103');
		}
		userSelector2.show();
	};
	var setUsers2 = function(o){
		var names = [];
		selectUserId2 =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			selectUserId2.push(o[i].id);
		}
		projectAssigner.setValue(names.join(';'));
	};
};